<template>
  <div class='top-category'>
    <div class="container">
      <!-- 面包屑 -->
     <xtx-bread separator='>'>
       <xtx-bread-item to='/'>主页</xtx-bread-item>
       <xtx-bread-item to="/categroy/1">电器</xtx-bread-item>
     </xtx-bread>
     <!-- 轮播图 -->
     <XtxCarousel :sliders="sliders" style="height:500px" />
     <!-- 所有二级分类 -->
     <div class="sub-list">
       <h3>全部分类</h3>
       <ul>
         <li v-for="item in cate.children" :key='item.id'>
           <a href="javascript:;">
             <img :src="item.picture"  alt="">
             <p>{{item.name}}</p>
           </a>
         </li>
       </ul>
     </div>
            <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="sub in cate.children" :key="sub.id">
          <div class="head">
            <h3>-  {{sub.name}}  -</h3>
            <XtxMore to="/" />
          </div>
          <div class="body">
            <GoodsItem
            v-for="good in sub.goods"
            :key="good.id"
            :good="good" ></GoodsItem>
            <!-- <RouterLink to="/" class="goods-item" v-for="good in sub.goods" :key="good.id">
              <img
                :src="good.picture"
                alt=""
              />
              <p class="name ellipsis">{{good.name}}</p>
              <p class="desc ellipsis">{{good.desc}}</p>
              <p class="price">&yen;{{good.price}}</p>
            </RouterLink> -->
          </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findBanner } from '@/api/home'
import { onMounted, ref } from 'vue'
import xtxBread from '@/components/xtx-bread.vue'
import XtxBreadItem from '@/components/xtx-bread-item.vue'
import { findTopCategory } from '@/api/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import GoodsItem from './components/goods-item.vue'
export default {
  components: { xtxBread, XtxBreadItem, GoodsItem },
  name: 'TopCategory',
  setup () {
    const sliders = ref([])

    // const cateList = ref([])
    // const cateName = ref('')
    // findTopCategory('1005002').then(data => {
    //   cateList.value = data.result.children
    //   cateName.value = data.name
    // })
    const route = useRoute()
    const cate = ref({})
    const loadData = (id) => {
      findBanner().then(data => {
        sliders.value = data.result
      })
      findTopCategory(id).then(data => {
        console.log('findTopCategory', data)
        cate.value = data.result
      })
    }
    // 只执行一次
    onMounted(() => {
      loadData(route.params.id)
    })
    // 路由变化时执行
    // to 要跳转的路由对象
    onBeforeRouteUpdate((to, from, next) => {
      console.log('onBeforeRouteUpdate...', to)
      loadData(to.params.id)
      next()
    })
    // onBeforeRouteUpdate(to=>{
    //   goodList.value=[]
    //   reqParams.page=1
    //   isFinished.value=false
    //   reqParams.categoryId=to.params.id
    //   loadData()
    // })
    return { sliders, cate } // cateName, cateList
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
